package com.ideaaedi.springcloud.jd.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.ideaaedi.springcloud.jd.commonds.constant.BaseConstant;
import com.ideaaedi.springcloud.jd.commonds.entity.user.po.SysUserGroupRePO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.SysGroupBindUserReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.SysGroupQueryUserReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.SysUserListReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.resp.SysUserListRespVO;
import com.ideaaedi.springcloud.jd.commonspring.mybatisplus.ServiceImplExt;
import com.ideaaedi.springcloud.jd.user.mapper.SysUserGroupReMapper;
import com.ideaaedi.springcloud.jd.user.service.SysUserGroupReService;
import com.ideaaedi.springcloud.jd.user.service.SysUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统-用户角色中间 服务实现类
 * </p>
 *
 * @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img
 * src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />
 * @since 2021.0.5.B
 */
@Service
public class SysUserGroupReServiceImpl extends ServiceImplExt<SysUserGroupReMapper, SysUserGroupRePO> implements SysUserGroupReService {
    
    @Resource
    private SysUserService sysUserService;
    
    @Resource
    private SysUserGroupReService sysUserGroupReService;
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<SysUserListRespVO> bindUser(SysGroupBindUserReqVO req) {
        Long groupId = req.getGroupId();
        Set<Long> userIds = req.getUserIds();
        
        // 删除旧的
        remove(new LambdaUpdateWrapper<SysUserGroupRePO>()
                .eq(SysUserGroupRePO::getGroupId, groupId)
        );
        
        // 新增新的
        List<SysUserGroupRePO> poList = userIds.stream().map(x -> {
            SysUserGroupRePO po = new SysUserGroupRePO();
            po.setId(IdWorker.getId());
            po.setGroupId(groupId);
            po.setUserId(x);
            return po;
        }).collect(Collectors.toList());
        saveBatch(poList);
        
        // 查询详情
        SysGroupQueryUserReqVO sysGroupQueryUserReq = new SysGroupQueryUserReqVO();
        sysGroupQueryUserReq.setGroupId(groupId);
        return queryUser(sysGroupQueryUserReq);
    }
    
    @Override
    public List<SysUserListRespVO> queryUser(SysGroupQueryUserReqVO req) {
        Long groupId = req.getGroupId();
        Set<Long> userIdSet = sysUserGroupReService.list(new LambdaQueryWrapper<SysUserGroupRePO>()
                .select(SysUserGroupRePO::getUserId)
                .eq(SysUserGroupRePO::getGroupId, groupId)
        ).stream().filter(Objects::nonNull).map(SysUserGroupRePO::getUserId).collect(Collectors.toSet());
        if (CollectionUtils.isEmpty(userIdSet)) {
            return Collections.emptyList();
        }
        SysUserListReqVO sysUserListReq = new SysUserListReqVO();
        sysUserListReq.setIdColl(userIdSet);
        sysUserListReq.setPageSize(BaseConstant.IGNORE_PAGING);
        return sysUserService.list(sysUserListReq).getDataList();
    }
}
